/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package practiceexamples;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author rdisalvo
 */
public class SideScrollingGUI extends JFrame implements ActionListener {

    JButton leftScroll = new JButton("⟸");
    JButton rightScroll = new JButton("⟹");
    JPanel centralPanel = new JPanel();
    JPanel firstPanel = new JPanel();
    JPanel secondPanel = new JPanel();
    JPanel thirdPanel = new JPanel();
    JLabel firstPanelL = new JLabel("FIRST PANEL");
    JLabel secondPanelL = new JLabel("SECOND PANEL");
    JLabel thirdPanelL = new JLabel("!");
    ImageIcon apple = new ImageIcon("green_apple.jpg");
    JLabel appleLabel = new JLabel("Now doesn't this just remind you of this.");
    {
        appleLabel.setIcon(apple);
    }
    JLabel hiMikeLabel = new JLabel("HI MIKE!");
    int currentPanel = 1;

    public SideScrollingGUI(String s) {
        super(s);
        leftScroll.setPreferredSize(new java.awt.Dimension(60, 600));
        rightScroll.setPreferredSize(new java.awt.Dimension(60, 600));
        this.add(leftScroll, BorderLayout.WEST);
        this.add(rightScroll, BorderLayout.EAST);
        this.setSize(800, 600);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        centralPanel.setLayout(null);
        this.add(centralPanel, BorderLayout.CENTER);
        firstPanel.add(firstPanelL);
        secondPanel.add(secondPanelL);
        secondPanel.add(hiMikeLabel);
        thirdPanel.add(thirdPanelL);
        thirdPanel.add(appleLabel);
        centralPanel.add(firstPanel);
        centralPanel.add(secondPanel);
        centralPanel.add(thirdPanel);
        // calculations:
        // top=29,left=1,bottom=1,right=1
        // central panel is : WIDTH: 800 - 60 - 60 - 1 - 1 = 678
        // HEIGHT: 800 - 29 - 1 = 800 - 30 = 770

        firstPanel.setBounds(0, 0, 678, 770);
        secondPanel.setBounds(678, 0, 678, 770);
        thirdPanel.setBounds(678 * 2, 0, 678, 770);
        firstPanel.setBackground(Color.BLUE);
        secondPanel.setBackground(Color.RED);
        thirdPanel.setBackground(Color.GREEN);
    }

    public void initListeners() {
        this.leftScroll.addActionListener(this);
        this.rightScroll.addActionListener(this);
    }

    public static void main(String[] argv) {
        SideScrollingGUI g = new SideScrollingGUI("Side Scrolling GUI");
        g.setVisible(true);
        g.initListeners();
        System.out.println(g.getInsets());

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == leftScroll) {
            Thread t = new Thread(new ScrollPanelRun(false));
            t.start();
        }
        if (e.getSource() == rightScroll) {
            Thread t = new Thread(new ScrollPanelRun(true));
            t.start();
        }
    }

    class ScrollPanelRun implements Runnable {

        boolean right;

        public ScrollPanelRun(boolean right) {
            this.right = right;
        }

        public void run() {
            double theCount = -4;

            if (right) {
                if (currentPanel == 1 || currentPanel == 2) {
                    currentPanel++;
                    // shift all three panels leftwards by 678 pixels
                    for (int i = 0; i < 67; i++) {
                        theCount += (8d / 67d);
                        try {
                            Thread.sleep((int) Math.pow(theCount, 2));
                        } catch (InterruptedException ex) {
                            Logger.getLogger(SideScrollingGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        try {
                            javax.swing.SwingUtilities.invokeAndWait(new Runnable() {

                                public void run() {
                                    Rectangle r = firstPanel.getBounds();
                                    firstPanel.setBounds(r.x - 10, 0, 678, 770);
                                    r = secondPanel.getBounds();
                                    secondPanel.setBounds(r.x - 10, 0, 678, 770);
                                    r = thirdPanel.getBounds();
                                    thirdPanel.setBounds(r.x - 10, 0, 678, 770);
                                    centralPanel.repaint();
                                }
                            });
                        } catch (InterruptedException ex) {
                            Logger.getLogger(SideScrollingGUI.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InvocationTargetException ex) {
                            Logger.getLogger(SideScrollingGUI.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    javax.swing.SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            Rectangle r = firstPanel.getBounds();
                            firstPanel.setBounds(r.x - 8, 0, 678, 770);
                            r = secondPanel.getBounds();
                            secondPanel.setBounds(r.x - 8, 0, 678, 770);
                            r = thirdPanel.getBounds();
                            thirdPanel.setBounds(r.x - 8, 0, 678, 770);
                        }
                    });
                } else if (currentPanel == 3) {
                    return;
                }

            } else { //left
                if (currentPanel == 1) {
                    return;
                } else if (currentPanel == 2 || currentPanel == 3) {
                    currentPanel--;
                    // shift all three panels rightward by 678 pixels
                            for (int i = 0; i < 67; i++) {
                                theCount += (8d / 67d);
                                try {
                                    Thread.sleep((int) Math.pow(theCount, 2));
                                } catch (InterruptedException ex) {
                                    Logger.getLogger(SideScrollingGUI.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                try {
                                    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {

                                        public void run() {
                                            Rectangle r = firstPanel.getBounds();
                                            firstPanel.setBounds(r.x + 10, 0, 678, 770);
                                            r = secondPanel.getBounds();
                                            secondPanel.setBounds(r.x + 10, 0, 678, 770);
                                            r = thirdPanel.getBounds();
                                            thirdPanel.setBounds(r.x + 10, 0, 678, 770);
                                            centralPanel.repaint();
                                        }
                                    });
                                } catch (InterruptedException ex) {
                                    Logger.getLogger(SideScrollingGUI.class.getName()).log(Level.SEVERE, null, ex);
                                } catch (InvocationTargetException ex) {
                                    Logger.getLogger(SideScrollingGUI.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                                public void run() {
                                    Rectangle r = firstPanel.getBounds();
                                    firstPanel.setBounds(r.x + 8, 0, 678, 770);
                                    r = secondPanel.getBounds();
                                    secondPanel.setBounds(r.x + 8, 0, 678, 770);
                                    r = thirdPanel.getBounds();
                                    thirdPanel.setBounds(r.x + 8, 0, 678, 770);
                                }
                            });
                        }
               
                }

            }

        }
    }

